﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CS2102_Music.Logic.LogicModels;

namespace CS2102_Music.Controllers
{
    public class ProductsController : Controller
    {
        //
        // GET: /Products/
        private Logic.Logic logic = Logic.Logic.GetInstance(new Logic.DbConnector());

        public ActionResult Index() //New in
        {
     
            // user types
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if(user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }



            // get products from database
            ViewBag.albums = logic.GetNewAlbums();
            ViewBag.songs = logic.GetNewSongs();

            ViewBag.typeSong = 1;
            ViewBag.typeAlbum = 2;
          
            return View();
        }
        public ActionResult BestSellers()
        {
            
            // user type
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }


            ViewBag.typeSong = 1;
            ViewBag.typeAlbum = 2;


            ViewBag.albums = logic.BestSellersAlbums();
            ViewBag.songs = logic.BestSellersSongs();

            return View();
        }
        public ActionResult Songs()
        {
           
            // user types
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }

            ViewBag.typeSong = 1;

            ViewBag.songs = logic.GetAllSongs();

            return View();
        }
        public ActionResult Albums()
        {
          
            // user type
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }

            ViewBag.typeSong = 1;
            ViewBag.typeAlbum = 2;

            ViewBag.albums = logic.GetAllAlbums();

            return View();
        }


        public ActionResult HottestAlbum()
        {
            // user type
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }

            ViewBag.typeSong = 1;
            ViewBag.typeAlbum = 2;

            ViewBag.albums = logic.HottestAlbum();

            return View();
        } //best selling album (1)
        public ActionResult HottestSong()
        {
            // user type
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }


            ViewBag.typeSong = 1;

            ViewBag.songs = logic.HottestSong();

            return View();
        } // best selling song (1)
        public ActionResult Below10()
        {

            // user type
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }


            ViewBag.typeSong = 1;
            ViewBag.typeAlbum = 2;

            ViewBag.albums = logic.Below10Deals();

            return View();
        }

        public ActionResult SearchSong(string search, string filter)
        {
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }

            ViewBag.typeSong = 1;
            ViewBag.typeAlbum = 2;

            ViewBag.songs = logic.SearchSong(search, filter);

            return View();
        }
        public ActionResult SearchAlbum(string search)
        {
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }

            ViewBag.typeSong = 1;
            ViewBag.typeAlbum = 2;


            ViewBag.albums = logic.SearchAlbum(search);

            return View();
        }

        public ActionResult ViewSong(int songID)
        {
            // user type
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }

            SongL songDetails = new SongL();
            songDetails = logic.GetSongByID(songID);
            ViewBag.date = (songDetails.ReleaseDate.Date).ToString("d");
            ViewBag.song = songDetails;

            ViewBag.typeSong = 1;

            return View();
        }
        public ActionResult ViewAlbum(int albumID)
        {
            AlbumL albumDetails = new AlbumL();
            albumDetails = logic.GetAlbumByID(albumID);
            ViewBag.album = albumDetails;
            ViewBag.date = (albumDetails.ReleaseDate.Date).ToString("d");
            

            // user type
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }

            ViewBag.typeSong = 1;
            ViewBag.typeAlbum = 2;

            return View();
        }



        public ActionResult EditSong(int songID)
        {
            // get song details
            SongL songDetails = new SongL();
            songDetails = logic.GetSongByID(songID);
            ViewBag.song = songDetails;
            string releaseDate = Logic.Utilities.ToHtmlDateString(songDetails.ReleaseDate);
            ViewBag.date = releaseDate;

            //get all artists
            List<ArtistL> list = new List<ArtistL>();
            list = logic.GetAllArtist();
            ViewBag.artists = list;


            return View();
        }
        public string EditSongAjax(int songID, string title, string releaseDate, double price, string genre, string composer, string lyricist, int duration, string artists, bool changeArtists)
        {
            string status = "Successfully updated song";

            //split artists by ";" & update artist sings     
            string[] list = artists.Split(';');

            logic.UpdateSong(songID, title, releaseDate, price, genre, composer, lyricist, duration, list, changeArtists);

            return status;
        }
        public string DeleteSongAjax(int songID)
        {
            string status = "Not successful deletion.";
            bool delete = false;

            delete = logic.DeleteSong(songID);

            if (delete)
                status = "Sucessfully deleted song.";
           
            return status;
        }


        public ActionResult EditAlbum(int albumID)
        {
            // get album details
            AlbumL albumDetails = new AlbumL();
            albumDetails = logic.GetAlbumByID(albumID);
            ViewBag.album = albumDetails;
            string releaseDate = Logic.Utilities.ToHtmlDateString(albumDetails.ReleaseDate);
            ViewBag.date = releaseDate;

            //get all songs
            List<SongL> list = new List<SongL>();
            list = logic.GetSongsNotInAlbum(albumID);
            ViewBag.songs = list;

            return View();
        }
        public string EditAlbumAjax(int albumID, string title, string releaseDate, double price, bool addSong, int newSongID)
        {
            string status = "Edit saved";

            logic.EditAlbumDetails(albumID, title, releaseDate, price);

            if (addSong)
                logic.AddSongToAlbum(albumID, newSongID);


            return status;
        }
        public string DeleteAlbumAjax(int albumID)
        {
            string status = "Not successful deletion.";
            bool delete = false;

            delete = logic.DeleteAlbum(albumID);

            if (delete)
                status = "Sucessfully deleted album.";

            return status;
        }

        public string DeleteSongFromAlbumAjax(int albumID, int songID)
        {
            string status = "Successfully deleted song from album!";

            bool delete = false;

            delete = logic.DeleteSongFromAlbum(albumID, songID);

            if (!delete)
                status = "Error in deletion (it is not allowed to delete this song as there is only 1 song left. There must be a song.";

            return status;
        }


        public ActionResult Create()
        {
            //get ALL artists
            List<ArtistL> list = new List<ArtistL>();
            list = logic.GetAllArtist();
            ViewBag.artists = list;

            //get ALL songs
            List<SongL> songList = new List<SongL>();
            songList = logic.GetAllSongs();
            ViewBag.songs = songList;

            return View();
        }


        public string CreateSongAjax(string title, string releaseDate, double price, string genre, string composer, string lyricist, int duration, string artists)
        {
            string status = "Song created successfully.";

            //split artists by ";" & update artist sings     
            string[] list = artists.Split(';');

            logic.CreateSong(title, releaseDate, price, genre, composer, lyricist, duration, list);

            return status;
        }
        public string CreateAlbumAjax(string title, string releaseDate, double price, int songID)
        {
            string status = "Successfully created!";

            logic.CreateAlbum(title, releaseDate, price, songID);

            return status;
        }



        public ActionResult Statistics()
        {
            int number;
            List<int> sales = new List<int>();

            ViewBag.WorstSellingAlbum = logic.WorstSellingAlbum(out number);
            ViewBag.wsaNum = number;



            ViewBag.worseSellingSong = logic.WorstSellingSong(out number);
            ViewBag.wssNum = number;

            ViewBag.deletedAlbums = logic.GetDeletedAlbums();
            ViewBag.deletedSongs = logic.GetDeletedSongs();


            ViewBag.top5Artists = logic.Top5Artists(out sales);
            ViewBag.t5ANum = sales;

            ViewBag.totalSpentCustomer = logic.TotalSpendCustomer(); //buyer email, total amount spent

            return View();
        }

        public string CreateArtistAjax(string name)
        {
            string status = "Successfully created!";

           logic.CreateArtist(name);

            return status;
        }
       
        

    }
}
